<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends CI_Controller {
	
	function __construct()
        {
		parent::__construct();
		$this->load->model('User');
	}

	function index()
	{
		$data['users']= $this->User->get_list();
		$this->load->view('layout/header') ; 
	        $this->load->view('user/user_list',$data);
		$this->load->view('layout/footer');
	}
	
	function profile($id)
	{
	 
		$data['user']= $this->User->get_data($id);
		$this->load->view('layout/header') ; 
	        $this->load->view('user/profile',$data);
		$this->load->view('layout/footer');
	}
	
	function newuser()
	{	 
		  $this->form_validation->set_rules('username', 'Username', 'required');
		  $this->form_validation->set_rules('pass', 'Password', 'required|matches[pass2]');
		  $this->form_validation->set_rules('pass2', 'Password Confirmation', 'required');
		  $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
		  
		  if($this->form_validation->run() == FALSE)
		  {
			 $this->load->view('layout/header') ; 
			 $this->load->view('login');
			 $this->load->view('layout/footer');
		  }
		  else
		  {	$var = $this->User->insert($this->input->post());
			   if($var != false)
				    {
					     $userData = array('mail'=>$var['email'],'pass1'=>$var['password']);
					     $var = ($this->User->ValidateSession($userData));
					     if($var != 0)
					     {
						      $this->session->set_userdata($var);
						      redirect("/");
					     }
					     else
					     {
						      $this->load->view('layout/header') ; 
						      $this->load->view('layout/menu');
						      $this->load->view('layout/footer');      
					     }
				    }
				    else
				    {
					     $this->load->view('layout/header') ; 
					     $this->load->view('layout/menu');
					     $this->load->view('layout/footer');
				    }
		  }
	}
	
}
